package com.tigercel.xiangchengyun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tigercel.xiangchengyun.R;
import com.tigercel.xiangchengyun.entity.TopNewsEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by wqj on 2018/5/25.
 */
public class TopNewAdapter extends BaseAdapter {
    private LayoutInflater mInflater = null;
    private Context mContext;
    private List<TopNewsEntity> mTopNewsEntityList;

    public TopNewAdapter(Context context, List<TopNewsEntity> list) {
        super();
        this.mContext = context;
        this.mTopNewsEntityList = list;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mTopNewsEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_top_news, null);
            viewHolder.img = convertView.findViewById(R.id.top_image);
            viewHolder.title = convertView.findViewById(R.id.top_title);
            viewHolder.comeFrom = convertView.findViewById(R.id.top_news_source);
            viewHolder.time = convertView.findViewById(R.id.top_time);
            viewHolder.readTimes = convertView.findViewById(R.id.tv_look_up);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.title.setText(mTopNewsEntityList.get(position).getTitle());
        viewHolder.comeFrom.setText(mTopNewsEntityList.get(position).getAuthor());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String d = sdf.format(new Date(mTopNewsEntityList.get(position).getPub_time() * 1000L));
        viewHolder.time.setText(d);

        if (mTopNewsEntityList.get(position).getThumb_img().equals("null")) {
            viewHolder.img.setVisibility(View.GONE);
        } else {
            Glide.with(mContext).load(mTopNewsEntityList.get(position).getThumb_img()).into(viewHolder.img);
        }

        return convertView;
    }

    static class ViewHolder {
        public ImageView img;
        public TextView title;
        public TextView time;
        public TextView comeFrom;
        public TextView readTimes;
    }
}
